package com.ruoyix.weblog.controller;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyix.common.annotation.RepeatSubmit;
import com.ruoyix.common.annotation.Log;
import com.ruoyix.common.core.controller.BaseController;
import com.ruoyix.common.core.domain.PageQuery;
import com.ruoyix.common.core.domain.R;
import com.ruoyix.common.core.validate.AddGroup;
import com.ruoyix.common.core.validate.EditGroup;
import com.ruoyix.common.core.validate.QueryGroup;
import com.ruoyix.common.enums.BusinessType;
import com.ruoyix.common.utils.poi.ExcelUtil;
import com.ruoyix.weblog.domain.vo.BlogFamilyVo;
import com.ruoyix.weblog.domain.bo.BlogFamilyBo;
import com.ruoyix.weblog.service.IBlogFamilyService;
import com.ruoyix.common.core.page.TableDataInfo;

/**
 * 家庭信息
 *
 * @author ruoyi
 * @date 2024-05-27
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/weblog/family")
public class BlogFamilyController extends BaseController {

    private final IBlogFamilyService iBlogFamilyService;

    /**
     * 查询家庭信息列表
     */
    @SaCheckPermission("weblog:family:list")
    @GetMapping("/list")
    public TableDataInfo<BlogFamilyVo> list(BlogFamilyBo bo, PageQuery pageQuery) {
        return iBlogFamilyService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出家庭信息列表
     */
    @SaCheckPermission("weblog:family:export")
    @Log(title = "家庭信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(BlogFamilyBo bo, HttpServletResponse response) {
        List<BlogFamilyVo> list = iBlogFamilyService.queryList(bo);
        ExcelUtil.exportExcel(list, "家庭信息", BlogFamilyVo.class, response);
    }

    /**
     * 获取家庭信息详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("weblog:family:query")
    @GetMapping("/{id}")
    public R<BlogFamilyVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iBlogFamilyService.queryById(id));
    }

    /**
     * 新增家庭信息
     */
    @SaCheckPermission("weblog:family:add")
    @Log(title = "家庭信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody BlogFamilyBo bo) {
        return toAjax(iBlogFamilyService.insertByBo(bo));
    }

    /**
     * 修改家庭信息
     */
    @SaCheckPermission("weblog:family:edit")
    @Log(title = "家庭信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody BlogFamilyBo bo) {
        return toAjax(iBlogFamilyService.updateByBo(bo));
    }

    /**
     * 删除家庭信息
     *
     * @param ids 主键串
     */
    @SaCheckPermission("weblog:family:remove")
    @Log(title = "家庭信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iBlogFamilyService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
